export default {
    'edc.excelView.tableInsertCombineRow': function () {
        const sheet = this.workbook.getActiveSheet();
        let row = sheet.getActiveRowIndex();
        const col = sheet.getActiveColumnIndex();
        const table = sheet.tables.find(row, col);
        if (table) {
            const actionName = 'edc.excelView.tableInsertCombineRow';
            const span = sheet.getSpan(row, col);
            if (span) {
                row = span.row + span.rowCount - 1;
            }
            this.workbook.commandManager().execute({
                autoFitType: GC.Spread.Sheets.AutoFitType.cell,
                cmd: actionName,
                sheet,
                sheetName: sheet.name(),
                row,
                col,
                table,
                store: this
            });
        }
    },
    'edc.excelView.tableInsertTopRow': function () {
        const sheet = this.workbook.getActiveSheet();
        let row = sheet.getActiveRowIndex();
        const col = sheet.getActiveColumnIndex();
        const table = sheet.tables.find(row, col);
        if (table) {
            const { row: startRow, col: startCol, rowCount, colCount } = table.dataRange();
            if (row > startRow) {
                for (let index = col - 1; index >= startCol; index--) {
                    const span = sheet.getSpan(row, index);
                    if (span && span.row < row) {
                        const actionName = 'edc.excelView.tableInsertCombineRow';
                        this.workbook.commandManager().execute({
                            autoFitType: GC.Spread.Sheets.AutoFitType.cell,
                            cmd: actionName,
                            sheet,
                            sheetName: sheet.name(),
                            row: row - 1,
                            col: span.col,
                            table,
                            store: this
                        });
                        return;
                    }
                }
            }

            const actionName = 'edc.excelView.tableInsertRow';
            this.workbook.commandManager().execute({
                autoFitType: GC.Spread.Sheets.AutoFitType.cell,
                cmd: actionName,
                sheet,
                sheetName: sheet.name(),
                row: row - 1,
                table,
                store: this
            });
        }
    },
    'edc.excelView.tableInsertBottomRow': function (workbook, paramRow, paramCol) {
        const sheet = this.workbook.getActiveSheet();
        let row = paramRow ? paramRow : sheet.getActiveRowIndex();
        const col = paramCol ? paramCol : sheet.getActiveColumnIndex();
        const table = sheet.tables.find(row, col);
        if (table) {
            const { row: startRow, col: startCol, rowCount, colCount } = table.dataRange();
            const span = sheet.getSpan(row, col);
            let rangeRowCount = 1;
            if (span) {
                row = row + span.rowCount - 1;
            }

            if (row < startRow + rowCount - 1) {
                for (let index = col - 1; index >= startCol; index--) {
                    const span = sheet.getSpan(row, index);
                    if (span && ((span.row + span.rowCount) > (row + 1))) {
                        const actionName = 'edc.excelView.tableInsertCombineRow';
                        this.workbook.commandManager().execute({
                            autoFitType: GC.Spread.Sheets.AutoFitType.cell,
                            cmd: actionName,
                            sheet,
                            sheetName: sheet.name(),
                            row,
                            col: span.col,
                            table,
                            store: this
                        });
                        return;
                    }
                }
            }

            const actionName = 'edc.excelView.tableInsertRow';
            this.workbook.commandManager().execute({
                autoFitType: GC.Spread.Sheets.AutoFitType.cell,
                cmd: actionName,
                sheet,
                sheetName: sheet.name(),
                row: row,
                table,
                store: this
            });
        }
    },
    'edc.excelView.tableDeleteRow': function (workbook, paramsRow, paramsCol) {
        const sheet = this.workbook.getActiveSheet();
        const row = paramsRow || sheet.getActiveRowIndex()
        const col = paramsCol || sheet.getActiveColumnIndex()
        const table = sheet.tables.find(row, col);
        if (table) {
            const actionName = 'edc.excelView.tableDeleteRow';
            this.workbook.commandManager().execute({
                autoFitType: GC.Spread.Sheets.AutoFitType.cell,
                cmd: actionName,
                sheet,
                sheetName: sheet.name(),
                row: row,
                table,
                store: this
            });
        }
    }
}